package com.example.myapplication;

import android.annotation.SuppressLint;
import android.media.MediaPlayer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.myapplication.R;
import java.util.List;

public class MusicAdapter extends RecyclerView.Adapter<MusicAdapter.MusicViewHolder> {

    private List<Song> musicList;  // 使用 Song 对象的列表
    private OnItemClickListener listener;
    private int currentPlayingPosition = -1;  // 记录当前播放的歌曲位置
    private MediaPlayer mediaPlayer;

    private static final String TAG = "MusicAdapter";  // 添加 TAG

    // 点击事件监听器接口
    public interface OnItemClickListener {
        void onItemClick(Song song);  // 当点击歌曲时调用
    }

    // 构造函数，传入音乐列表和点击事件监听器
    public MusicAdapter(List<Song> musicList, OnItemClickListener listener) {
        this.musicList = musicList;
        this.listener = listener;
    }

    @NonNull
    @Override
    public MusicViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 加载 item_music.xml 布局
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_music, parent, false);
        return new MusicViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MusicViewHolder holder, @SuppressLint("RecyclerView") int position) {
        // 获取当前歌曲对象
        Song song = musicList.get(position);

        holder.songTitle.setText(song.getTitle());  // 设置歌曲名称
        holder.songIcon.setImageResource(song.getIconResId());  // 设置封面图标

        // 处理播放/暂停按钮的点击事件
        holder.playPauseButton.setOnClickListener(v -> {
            if (position == currentPlayingPosition) {
                // 如果点击的是正在播放的歌曲，切换播放/暂停状态
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.pause();
                    holder.playPauseButton.setImageResource(R.drawable.ic_play);  // 设置为播放图标
                } else {
                    mediaPlayer.start();
                    holder.playPauseButton.setImageResource(R.drawable.ic_pause);  // 设置为暂停图标
                }
            } else {
                // 播放新的歌曲
                if (currentPlayingPosition != -1 && mediaPlayer.isPlaying()) {
                    // 停止并释放当前播放的歌曲
                    mediaPlayer.stop();
                    mediaPlayer.release();
                }
                currentPlayingPosition = position;

                // 获取歌曲资源 ID
                int trackResId = song.getResId();  // 通过 Song 对象获取资源ID

                // 初始化 MediaPlayer 并开始播放
                mediaPlayer = MediaPlayer.create(holder.itemView.getContext(), trackResId);
                mediaPlayer.setOnPreparedListener(mp -> {
                    mediaPlayer.start();  // 开始播放音乐
                    holder.playPauseButton.setImageResource(R.drawable.ic_pause);  // 设置为暂停图标
                });

                // 防止 MediaPlayer 无法播放
                mediaPlayer.setOnErrorListener((mp, what, extra) -> {
                    mp.reset();
                    return true;
                });
            }
        });
    }

    @Override
    public int getItemCount() {
        return musicList.size();  // 返回音乐列表的长度
    }

    public static class MusicViewHolder extends RecyclerView.ViewHolder {

        TextView songTitle;
        ImageButton playPauseButton;
        ImageView songIcon;

        public MusicViewHolder(View itemView) {
            super(itemView);
            songTitle = itemView.findViewById(R.id.songTitle);
            playPauseButton = itemView.findViewById(R.id.playPauseButton);
            songIcon = itemView.findViewById(R.id.songIcon);
        }
    }
}
